﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using _2013_ece_csharp_snake;

namespace WindowsFormsApplication1
{
    public partial class FormDemo : Form
    {
        private EventWaitHandle eventFin;
        private EventWaitHandle eventSend;
        private TcpListener tcpListener;

        private ConnexionSendMulti sender;
        private ConnexionReceiveMulti receiver;
        private ConnectionInformation coInf;

        private GameZone mainGame;
        private GameZone multiGame;

        private DialogCo dialogCo;

        private LoginForm login;
        private EndForm end;

        public DateTime intialTime { get; set; }

        public FormDemo(ConnectionInformation coInf)
        {
            InitializeComponent();
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.StartPosition = FormStartPosition.CenterScreen;

            //dialogCo = new DialogCo();
            //dialogCo.Visible = false;
            //coInf = new ConnectionInformation();
            this.coInf = coInf;

            //showLogin(coInf);

            this.labelFormDemoHelloUser.Text = "Hello " + coInf.myName;

            // Creation of the 2 game areas
            mainGame = new GameZone(panel1, this, GameZone.NORMAL, coInf.myName);
            multiGame = new GameZone(panel2, this, GameZone.IMPORTED, "constestant");

            eventFin = new EventWaitHandle(false, EventResetMode.AutoReset);
            eventSend = new EventWaitHandle(false, EventResetMode.AutoReset);

            sender = new ConnexionSendMulti(this, eventFin, eventSend, tcpListener, coInf);
            receiver = new ConnexionReceiveMulti(this, eventFin, eventSend, tcpListener, coInf);

            // Initializing the time
            this.intialTime = DateTime.Now;
        }

        // add shadow 
        // Creating a shadow on the border of the form
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams parameters = base.CreateParams;
                parameters.ClassStyle = (parameters.ClassStyle | 0x00020000);

                return parameters;
            }
        }

        // display end form when loose
        public static void startEndForm(Object score)
        {
            Application.Run(new EndForm((int) score));
        }
        
        private void timer1_Tick(object sender, EventArgs e)
        {

            mainGame.actualiser();
            multiGame.actualiser();
            this.uC_GameInfo1.updateScore(this.mainGame.Score);
            this.uC_GameInfo1.updateTime(this.intialTime);
        }

        // display received messages
        public delegate void receiveMessageDelegate(String s);
        public void receiveMessage(String s)
        {
            if (console.InvokeRequired)
            {
                console.Invoke(
                    new receiveMessageDelegate(receiveMessage), s);
            }
            else
            {
                console.Text = console.Text + Environment.NewLine + s;
                console.SelectionStart = console.Text.Length;
                console.ScrollToCaret();
            }
        }

        // Get the text to send
        public delegate string getTextToSendDelegate();
        public string getTextToSend()
        {
            if (textSend.InvokeRequired)
            {
                textSend.Invoke(
                    new getTextToSendDelegate(getTextToSend));
            }
            else
            {
                return textSend.Text;
            }

            return textSend.Text;
        }

        public delegate GameZone getGameZoneDelegate();
        public GameZone getGameZone()
        {
            if (textSend.InvokeRequired)
            {
                textSend.Invoke(
                    new getGameZoneDelegate(getGameZone));
            }
            else
            {
                return mainGame;
            }
            return mainGame;
        }

        public delegate GameZone getMultiGameDelegate();
        public GameZone getMultiGame()
        {
            if (textSend.InvokeRequired)
            {
                textSend.Invoke(
                    new getMultiGameDelegate(getMultiGame));
            }
            else
            {
                return multiGame;
            }

            return multiGame;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            eventSend.Set();
        }


        private void textSend_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                eventSend.Set();
            }
        }

        // Get mouvement
        public void moveManagement(KeyPressEventArgs e)
        {

            if (e.KeyChar == 'z' && !(mainGame.Snake.Dir.Y > 0))
                mainGame.Snake.goUp();
            else if (e.KeyChar == 's' && !(mainGame.Snake.Dir.Y < 0))
                mainGame.Snake.goDown();
            else if (e.KeyChar == 'q' && !(mainGame.Snake.Dir.X > 0))
                mainGame.Snake.goLeft();
            else if (e.KeyChar == 'd' && !(mainGame.Snake.Dir.X < 0))
                mainGame.Snake.goRight();

            // Pause
            if (e.KeyChar == 'p')
            {
                ResumePause();
            }
        }

        private void FormDemo_KeyPress(object sender, KeyPressEventArgs e)
        {
            moveManagement(e);
        }


        private void button1_Click_1(object sender, EventArgs e)
        {
            dialogCo = new DialogCo(coInf);
            dialogCo.Visible = true;

            timer1.Enabled = false;
        }

        private void labelReduce_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        // exit the application
        private void labelClose_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void labelPlayOnline_Click(object sender, EventArgs e)
        {
            dialogCo = new DialogCo(coInf);
            dialogCo.Visible = true;
        }

        private void labelSend_Click(object sender, EventArgs e)
        {
            eventSend.Set();
        }

        private void labelPause_Click(object sender, EventArgs e)
        {
            ResumePause();
        }

        private void ResumePause()
        {
                if (timer1.Enabled == false)
                {
                    this.labelPause.Text = "Pause";
                    timer1.Enabled = true;
                }
                else
                {
                    this.labelPause.Text = "Resume"; 
                    timer1.Enabled = false;
                }
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        // Move the window when click on blanck spaces
        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case 0x84:
                    base.WndProc(ref m);
                    if ((int)m.Result == 0x1)
                        m.Result = (IntPtr)0x2;
                    return;
            }

            base.WndProc(ref m);
        }

    }
}
